/**
 * iWeb2GooglePages - Converts export iWeb content and uploads it to GPs.
 * Copyright (C) 2008 Will Droste
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package org.iweb2gps;

import static org.jdesktop.application.Application.getInstance;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.util.prefs.BackingStoreException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.application.ApplicationContext;
import org.jdesktop.application.ResourceMap;

/**
 * All preferences for the account etc..
 */
@SuppressWarnings("serial")
public class PreferencesBox extends JDialog {

    private JButton browseButton;
    private JCheckBox domainCheckbox;
    private JTextField domainText;
    private JTextField exportDirectoryText;
    private JPasswordField passwordField;
    private JTextField userNameText;
    private JLabel domainLabel;
    private JLabel addressLabel;
    private AppPreferences prefs;

    /**
     * @param parent
     */
    public PreferencesBox(Frame parent) {
        super(parent);
        // get the application preferences
        try {
            prefs = new AppPreferences();
        } catch (BackingStoreException e) {
            throw new RuntimeException(e);
        }
        // initialize the display...
        initComponents();
        // add window close event..
        addWindowListener(new View.AbstractWindowListner() {
            @Override
            public void windowClosed(WindowEvent arg0) {
                // save each of the preferences.
                try {
                    prefs.setUserName(userNameText.getText());
                    prefs.setPassword(new String(passwordField.getPassword()));
                    if (domainCheckbox.isSelected()) {
                        prefs.setDomain(domainText.getText());
                    }
                    prefs.setExportDirectory(exportDirectoryText.getText());
                    prefs.sync();
                } catch (BackingStoreException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the display.
     */
    private void initComponents() {

        // get the application context..
        ApplicationContext ctx = getInstance(App.class).getContext();
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        // create the main container for configuration..
        ResourceMap resourceMap = ctx.getResourceMap(PreferencesBox.class);
        setTitle(resourceMap.getString("title"));
        setModal(true);
        setName("preferenceBox");
        setResizable(false);
        setLayout(new MigLayout("", "[right, fill]"));

        // get export directory..
        add(new JLabel(resourceMap.getString("googlepages.text")),
                "split, span, gaptop 10");
        JSeparator sep = new JSeparator();
        sep.setPreferredSize(new Dimension(350, 10));
        add(sep, "growx, wrap, gaptop 10");

        // username label/text field
        JLabel userNameLabel = new JLabel();
        userNameLabel.setText(resourceMap.getString("userNameLabel.text"));
        userNameLabel.setName("userNameLabel");
        add(userNameLabel, "gap 10");

        userNameText = new JTextField();
        userNameText.setText(prefs.getUserName());
        userNameText.setName("userNameText");
        add(userNameText, "wrap, growx");

        // password label/text field
        JLabel passwordLabel = new JLabel();
        passwordLabel.setText(resourceMap.getString("passwordLabel.text"));
        passwordLabel.setName("passwordLabel");
        add(passwordLabel, "gap 10");

        passwordField = new JPasswordField();
        passwordField.setText(prefs.getPassword());
        passwordField.setName("passwordField");
        add(passwordField, "wrap, growx");

        // domain check box..
        domainLabel = new JLabel();
        domainLabel.setText(resourceMap.getString("domainLabel.text"));
        domainLabel.setName("domainLabel");
        add(domainLabel, "gap 10");

        domainCheckbox = new JCheckBox();
        domainCheckbox.setText(resourceMap.getString("domainCheckbox.text"));
        domainCheckbox.setName("domainCheckbox");
        domainCheckbox.setSelected(prefs.getDomain() != null);
        domainCheckbox.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent evt) {
                domainCheckboxStateChanged(evt);
            }
        });
        add(domainCheckbox, "wrap, growx");

        // Address to the domain..
        addressLabel = new JLabel();
        addressLabel.setText(resourceMap.getString("addressLabel.text"));
        addressLabel.setName("addressLabel");
        add(addressLabel, "gap 10");

        domainText = new JTextField();
        domainText.setText(prefs.getDomain());
        domainText.setName("domainText");
        add(domainText, "wrap, span, growx");

        // iweb settings
        add(new JLabel(resourceMap.getString("iwebSetings.text")),
                "split, span, gaptop 10");
        add(new JSeparator(), "growx, wrap, gaptop 10");

        // iweb settings..
        JLabel exportLabel = new JLabel();
        exportLabel.setText(resourceMap.getString("exportLabel.text"));
        exportLabel.setName("addressLabel");
        add(exportLabel, "gap 10");

        exportDirectoryText = new JTextField();
        exportDirectoryText.setText(prefs.getExportDirectory());
        exportDirectoryText.setName("exportDirectoryText");
        add(exportDirectoryText, "growx, pushx");

        browseButton = new JButton();
        browseButton.setText(resourceMap.getString("browseButton.text"));
        browseButton.setName("browseButton");
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });
        add(browseButton, "wrap");

        pack();
    }

    private void domainCheckboxStateChanged(javax.swing.event.ChangeEvent evt) {
        boolean enable = domainCheckbox.isSelected();
        domainLabel.setEnabled(enable);
        domainText.setEnabled(enable);
        addressLabel.setEnabled(enable);
    }

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
        JFileChooser chooser = new JFileChooser(".");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int status = chooser.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
            exportDirectoryText.setText(chooser.getSelectedFile().toString());
        }
    }
}
